<?php
	header ('Content-type: text/html; charset=UTF-8'); 
	include ('../config/validacao.php');
	
	$ramal = '';
	$idsetor = 0;
	if(isset($_GET['id']) && isset($_GET['ramal'])){
	
		include ('../config/conexao.php');

		// query the database table for zip codes that match 'term'
		$rs = mysql_query('SELECT idcontato, nomeContato, cargo, email, tag FROM contato WHERE idcontato="'.$_GET['id'].'"');

		// loop through each zipcode returned and format the response for jQuery
		$dados = mysql_fetch_array($rs);
		
		
		$idcontato = $dados['idcontato'];
		$nomeContato = utf8_encode($dados['nomeContato']);
		$cargo = utf8_encode($dados['cargo']);
		$email = $dados['email'];
		$tag = $dados['tag'];
		
		// query the database table for zip codes that match 'term'
		$rs = mysql_query('SELECT idsecretaria, idramais, setor_idsetor FROM ramais,setor,secretaria WHERE idramais="'.$_GET['ramal'].'" AND `setor_idsetor`=idsetor AND secretaria_idsecretaria=idsecretaria ');

		// loop through each zipcode returned and format the response for jQuery
		$dados = mysql_fetch_array($rs);
		$idsetor = $dados['setor_idsetor'];
		$idsecretaria = $dados['idsecretaria'];
		$idramais = $dados['idramais'];
		
		
		
	}else{
		echo "<script>alert('Erro ao listar registro');</script>";
		echo "<script>window.location.href = 'e_ramal.php';</script>";
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"http://www.w3.org/TR/html4/strict.dtd">
<html lang="pt_BR">
<head>
	<meta charset="utf-8">
	<link rel="icon" type="image/png" href="../images/icone.png">
	<title><?php include('../titulo.php')?></title>
	<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
 	
	<link rel="stylesheet" href="../css/jquery-ui-1.8.21.custom.css" type="text/css" media="all" />
	<link rel="stylesheet" href="../css/index.css"  media="screen"> 
	<link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"  media="screen"> 
	
	<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script> 
	<script type="text/javascript" src="../js/bootstrap-inputmask.js"/></script>
	
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script> 
	<script type="text/javascript" language="javascript" >
		$(document).ready(function(){
			$('#form').validate({  
				rules: {  
					
					setor: { required: true },
					ramal: { required: true },
					email: { email: true }
					
				},  
				messages: {  
					setor: { required: 'Preencha o campo'},
					ramal: { required: 'Preencha o campo'}
					//"contato[]": { required: 'Preencha o campo'}
				},
				submitHandler: function( form ){  
					var dados = $( form ).serialize();  
					$.ajax({  
						type: "POST",  
						url: "update_contato.php",  
						data: dados,  
						success: function( data )  
						{  
							alert( data );
							window.location.href = "e_contato.php";
						}  
					});  
					return false;  
				} 
			}); 
			$("input#contato").each(function(){
			   $(this).rules("add", {required:true,messages:{required: 'Preencha o campo'}});                   
			});
			/*$("input#email").each(function(){
				$(this).rules("add", {email: true//,matches: '.+@arapongas.pr.gov.br',messages:{matches: 'Preencha o campo'}
				});
			});
			*/
		}); 
		$(function(){
			$("select#secretaria").change(function(){
			$.getJSON("../c_setor/seleciona_setor.php",{id: $(this).val(), ajax: 'true'}, function(j){
			  var options = '';
			  options += '<option value="0">Selecione um setor</option>';
			  ramal    = '<option value="0">AGUARDE...</option>';
			  for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			  }
			  $("select#setor").html(options);
			  $("select#ramal").html(ramal);
			})
			})
		})
		$(function(){
		  $("select#setor").change(function(){
			$.getJSON("../c_ramal/seleciona_ramal.php",{id: $(this).val(), ajax: 'true'}, function(j){
			  var options = '';
		//	  options += '<option value="0">Selecione um setor</option>';
			  for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			  }
			  $("select#ramal").html(options);
			})
		  })
		})
	</script>

</head>
<body onload="$('#consulta').select()">
	<div id="container">
		<div id="topo" style="text-align:left;position:absolute;">
			<?php include('../cabecalho.php') ?>
		</div>
		<br><br>
		<div id="body">
			<form id="form">
		<!--	<form id="form" action="c_contato.php" method="post"> -->
				<fieldset>
					<legend>Alterar Ramal</legend>
<label>Local</label>
<select id="secretaria" name="secretaria" class="span5">
<!--<option value="">SELECIONE LOCAL </option> -->
<?php
header ('Content-type: text/html; charset=UTF-8');
//include ('../config/conexao.php');

// query the database table for zip codes that match 'term'
$rs = mysql_query('SELECT idsecretaria, sigla, nomeSecretaria FROM secretaria
ORDER BY sigla ASC');

// loop through each zipcode returned and format the response for jQuery
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs) )
{
if($idsecretaria == $row['idsecretaria']){
echo '<option value='.$row["idsecretaria"].' SELECTED>'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSecretaria"]).'</option>';
}else{
echo '<option value='.$row["idsecretaria"].' >'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSecretaria"]).'</option>';
}

}
}
?>
</select>					
					<label>Setor</label>
					<select id="setor" name="setor">
						<option value="">AGUARDE...</option>
						<?php
							header ('Content-type: text/html; charset=UTF-8'); 
				//			include ('../config/conexao.php');
							 
							// query the database table for zip codes that match 'term'
							$rs = mysql_query('SELECT idsetor, sigla, nomeSetor FROM setor, secretaria
												WHERE  idsecretaria = secretaria_idsecretaria ORDER BY sigla ASC, nomeSetor ASC');

							// loop through each zipcode returned and format the response for jQuery
							$data = array();
							if ( $rs && mysql_num_rows($rs) )
							{
								while( $row = mysql_fetch_array($rs) )
								{
									if($idsetor == $row['idsetor']){
										echo '<option value='.$row["idsetor"].' SELECTED>'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSetor"]).'</option>';
									}else{
										echo '<option value='.$row["idsetor"].'>'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSetor"]).'</option>';
									}
								}
							}
						?>
					</select>
					<label>Ramal</label>
					<select id="ramal" name="ramal" class="span2">
						<?php
						
							 
							// query the database table for zip codes that match 'term'
							$rs = mysql_query('SELECT idramais, ramal FROM ramais WHERE setor_idsetor='.$idsetor);

							// loop through each zipcode returned and format the response for jQuery
							$data = array();
							if ( $rs && mysql_num_rows($rs) )
							{
								while( $row = mysql_fetch_array($rs) )
								{
									if($idramais == $row['idramais']){
										echo '<option value='.$row["idramais"].' SELECTED>'.$row["ramal"].'</option>';
									}else{
										echo '<option value='.$row["idramais"].'>'.$row["ramal"].'</option>';
									}
									
								}
							}
						?>
					</select>
					<br>
				</fieldset>
				<fieldset>
				<legend>Editar Contato</legend>
				<div class="contatos">
					<p class="camposcontatos">
						<input type="hidden" value="<?php echo $_GET['id']; ?>" name="idcontato"/>
						<label>Nome</label><input class="input-xlarge" id="contato" type="text" name="contato" value="<?php echo $nomeContato;?>" onChange="javascript:this.value=this.value.toUpperCase();"/>
						<label>Cargo</label><input class="input-xlarge" type="text" name="cargo" value="<?php echo $cargo;?>" onChange="javascript:this.value=this.value.toUpperCase();"/>
						<label>E-mail</label><input class="input-xlarge" type="text" name="email" value="<?php echo $email;?>" />
						<label>Tag</label><input class="input-xlarge" type="text" name="tag" value="<?php echo $tag;?>" onChange="javascript:this.value=this.value.toUpperCase();"/>
						<a href="#" class="removerCampo">
					</p>
				</div>
				<br>
				<button type="submit" class="btn">Atualizar</button>
				</fieldset>
				</form>
		</div>
	</div>
</body>
</html>
